package com.lidy.tankbattle.tankutils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lidy.tankbattle.tankgame2.wall.Wall;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lidy
 * @version V1.0
 * @classname WallUtils
 * @description 墙体工具类
 * @date 2022/6/6 20:03
 */
public class WallUtils {

    public static final ObjectMapper JACK_SON = new ObjectMapper();
    public static void main(String[] args) {
        makeWallPicture1();
        List<Wall> walls = readWallPicture1();
        System.out.println(walls);
    }

    /**
     * 生成墙体图
     **/
    public static void makeWallPicture1() {
        int number = 10000;
        List<Wall> walls = new ArrayList<>(80);
        for (int i = 0; i < 12; i++) {
            if (i % 2 == 0) {
                continue;
            }
            int x = i * 50, y = 100;
            for (int j = 0; j < 8; j++) {
                walls.add(Wall.getBrickWall(number++, x, y));
                y += 50;
            }
        }
        walls.add(Wall.getMetalWall(number++, 200, 200));
        walls.add(Wall.getMetalWall(number++, 400, 200));
        for (int i = 0; i < 3; i++) {
            walls.add(Wall.getBrickWall(number++, 100 + i * 200, 350));
        }
        for (int i = 0; i < 7; i++) {
            walls.add(Wall.getBrickWall(number++, (i + 1) * 50, 550));
        }
        walls.add(Wall.getMetalWall(number++, 500, 550));
        for (int i = 0; i < 7; i++) {
            walls.add(Wall.getBrickWall(number++, (i + 1) * 50 + 150, 650));
        }
        walls.add(Wall.getMetalWall(number, 100, 750));
        File file = Paths.get(System.getProperty("user.dir") + "/src/main/resources/walls/wallpicture1.txt").toFile();
        try {
            JACK_SON.writeValue(file, walls);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @author: lidy
     * @description: 读取墙体图1
     * @date: 2022/6/6 20:05
     */
    public static List<Wall> readWallPicture1() {
        File file = Paths.get(System.getProperty("user.dir") + "/src/main/resources/walls/wallpicture1.txt").toFile();
        List<Wall> walls;
        try {
            walls = JACK_SON.readValue(file, new TypeReference<List<Wall>>(){});
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return walls;
    }
}
